﻿
  function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object

    // Loop through the FileList and render image files as thumbnails.
    for (var i = 0, f; f = files[i]; i++) {

      // Only process image files.
      if (!f.type.match('image.*')) {
        continue;
      }

      var reader = new FileReader();

      // Closure to capture the file information.
      reader.onload = (function(theFile) {
        return function(e) {
          // Render thumbnail.
          var span = document.createElement('span');
          $("#pictureAddNewSellAuctionDiv").html("<img id= 'pictureAddNewSellAuction' style='width:200px; height:200px' src='"+ e.target.result+"'></img>");
        };
      })(f);

      // Read in the image file as a data URL.
      reader.readAsDataURL(f);
    }
}
$(document).ready(function () {
    document.getElementById('imageAddNewSellAuctionInput').addEventListener('change', handleFileSelect, false);
});


function isRequired(val) {
    if (typeof (val) == "undefine"||val.trim() == "") {
        return false;
    } else {
        return true;
    }
}
function onChangeFileAddNewSellAuction() {
    alert($('#fileAddNewSellAuction').attr('file')[0]);
    $('#pictureAddNewSellAuction').attr('src', $('#fileAddNewSellAuction').attr('file')[0]);
}

function submitAddNewSellAuction() {
    
    var topic = $('#topicAddNewSellAuctionInput').val();
    var startedDate = $('#startedDateAddNewSellAuctionInput').val();
    var finishedDate = $('#finishedDateAddNewSellAuctionInput').val();
    var initialPrice = $('#initialPriceAddNewSellAuctionInput').val();
    var productId = $('#productAddNewSellAuctionSelection').val();
    var description = $('#productDescriptionAddNewSellAuctionInput').val();
    var selectedProduct;
    for (var i = 0; i < allProducts.length; i++) {
        if (allProducts[i].ProductId == productId) {
            selectedProduct = allProducts[i];
        }
    }
    $('#ErrorPanelAddNewSellAuctionDiv').html("");
    var ok = true;
    if (!isRequired(topic)) {
        $('#ErrorPanelAddNewSellAuctionDiv').append('Chủ đề còn trống<br>');
        ok = false;
    }
   
    if (!isRequired(startedDate)) {
        $('#ErrorPanelAddNewSellAuctionDiv').append('Thời gian bắt đầu còn trống<br>');
        ok = false;
    }
    if (!isRequired(finishedDate)) {
        $('#ErrorPanelAddNewSellAuctionDiv').append('Thời gian kết thúc còn trống<br>');
        ok = false;
    }
    if (!isRequired(description)) {
        $('#ErrorPanelAddNewSellAuctionDiv').append('Mô tả sản phẩm còn trống<br>');
        ok = false;
    }
    if (!isRequired(initialPrice) || initialPrice <= 0) {
        $('#ErrorPanelAddNewSellAuctionDiv').append('Giá khởi điểm không hợp lệ<br>');
        ok = false;
    }
    if (ok == true) {
        $('#topicAddNewSellAuctionInputHidden').val(topic);
        $('#startedDateAddNewSellAuctionInputHidden').val(startedDate);
        $('#finishedDateAddNewSellAuctionInputHidden').val(finishedDate);
        $('#initialPriceAddNewSellAuctionInputHidden').val(initialPrice);
        $('#productAddNewSellAuctionSelectionHidden').val(JSON.stringify(selectedProduct));
        $('#productDescriptionAddNewSellAuctionInputHidden').val(description);
        $('#imageAddNewSellAuctionForm').submit();
    }
}
